﻿

local wangxiang_generalSkill = fk.CreateActiveSkill{
  name = "wangxiang_general_skill",
  can_use = Util.FalseFunc,
}
local wangxiang_generalTriggerSkill = fk.CreateTriggerSkill{
  name = "wangxiang_general_trigger_skill",
  global = true,

  refresh_events = {fk.AfterCardsMove, fk.SkillEffect},
  can_refresh = function(self, event, target, player, data)
    if event == fk.AfterCardsMove then
      if player.seat == 1 then
        for _, move in ipairs(data) do
          if move.to and move.toArea == Card.PlayerHand then
            return true
          end
          if move.from then
            for _, info in ipairs(move.moveInfo) do
              if info.fromArea == Card.PlayerHand or info.fromArea == Card.PlayerSpecial then
                return true
              end
            end
          end
        end
      end
    elseif event == fk.SkillEffect then
      return target == player and table.contains(player:getTableMark("HQwangxiang_skills"), data.name) and
        data.name ~= "hanqing__qielv"  --挈律技能流程中就已经移牌了
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    if event == fk.AfterCardsMove then
      for _, p in ipairs(room.alive_players) do
        local new_skills = {}
        for _, id in ipairs(p:getCardIds("h&")) do
          local skill = Fk:getCardById(id):getMark("hanqing__wangxiang_skill")
          if skill ~= 0 and not p:hasSkill(skill, true) then
            table.insert(new_skills, skill)
          end
        end
        local skills = p:getTableMark("HQwangxiang_skills")
        for _, s in ipairs(skills) do
          if not table.contains(new_skills, s) then
            p:loseFakeSkill(s)
          end
        end
        for _, s in ipairs(new_skills) do
          if not table.contains(skills, s) then
            p:addFakeSkill(s)
            p:prelightSkill(s, true)
          end
        end
        room:setPlayerMark(p, "HQwangxiang_skills", new_skills)
      end
    elseif event == fk.SkillEffect then
      local card = table.find(player:getCardIds("h&"), function (id)
        local c = Fk:getCardById(id)
        return c.name == "wangxiang_general" and c:getMark("hanqing__wangxiang_skill") == data.name
      end)
      if card then
        room:moveCards({
          ids = {card},
          from = player.id,
          toArea = Card.Processing,
          moveReason = fk.ReasonUse,
        })
        room.logic:getCurrentEvent():addCleaner(function()
          if room:getCardArea(card) == Card.Processing then
            room:moveCards({
              ids = {card},
              toArea = Card.DiscardPile,
              moveReason = fk.ReasonUse,
            })
          end
        end)
      end
    end
  end,
}
local wangxiang_general = fk.CreateBasicCard{
  name = "&wangxiang_general",
  skill = wangxiang_generalSkill,
}
Fk:loadTranslationTable{
  ["wangxiang_general"] = "万象",
  [":wangxiang_general"] = "这张牌视为基本牌。<br/><b>效果</b>：你可以使用此牌以发动此牌记录的技能。",
}
